<template>
  <div class="container">
    <!-- header -->
    <div class="valueHeader">
      <van-icon @click="home" name="wap-home-o" size="25" />
      <div>值得买</div>
      <div class="valueHeaderRight">
        <van-icon @click="category" class="searchIcon" name="search" size="25" />
        <van-icon @click="shopCart" name="shopping-cart-o" size="25" />
      </div>
    </div>
    <!-- nav -->
    <div class="nav">
      <div class="navHeader">
        <img class="navTitleImg" src="http://yanxuan.nosdn.127.net/0b7676e645253f84be662aacfc051922.png" alt="" />
        <div class="navTitle">严选好物&nbsp;用心生活</div>
        <img class="navImg" src="http://yanxuan.nosdn.127.net/a93a392fb8006ba26dea38477979b7b4.jpg?imageView" alt="" srcset="" />
      </div>
      <!-- navMain -->
      <div class="navMainBox">
        <van-swipe class="navMainSwiper" indicator-color="black" :loop="false" :height="215" :width="88">
          <van-swipe-item class="swiperItem" v-for="item in navWap.length / 2" :key="item.id">
            <div class="swiperItemMain">
              <img class="swiperItemImg" :src="navWap[item - 1].picUrl" />
              <div class="swiperItemTittle">
                <div class="swiperItemTittleTop">好物大赏</div>
                <div class="swiperItemTittleBottom">购物先看榜</div>
              </div>
            </div>

            <div class="swiperItemMain">
              <img class="swiperItemImg" :src="navWap[item - 1 + navWap.length / 2].picUrl" />
              <div class="swiperItemTittle">
                <div class="swiperItemTittleTop">好物大赏</div>
                <div class="swiperItemTittleBottom">购物先看榜</div>
              </div>
            </div>
          </van-swipe-item>
          <!-- 指示器 -->
          <template #indicator="{ active, total }">
            <div class="custom-indicator">
              <div :class="{ active: item - 1 === active }" class="custom-indicatorItem" v-for="item in total" :key="item"></div>
            </div>
          </template>
        </van-swipe>
      </div>
    </div>

    <!-- masonry瀑布流 -->
    <van-list
      :immediate-check="false"
      @load="onLoad"
      offset="150"
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
    >
      <div class="masonryBox">
        <div gutter="10" origin-left="false" tem-selector=".item" class="masonryBoxMain" v-masonry>
          <!-- 单个盒子 -->

          <div class="item" v-masonry-tile v-for="item in nowRecAuto" :key="item.topicId">
            <div class="itemMain">
              <!-- 主要图片 -->
              <div class="itemMainImg">
                <img class="itemMainImgOne" :src="item.newAppBanner || item.picUrl" />
                <img
                  class="videoIcon"
                  v-if="item.lookPics && item.lookPics[0].isVideo"
                  src="https://yanxuan.nosdn.127.net/f631a249f001f3a9dfb46b7bfd723070.png"
                  alt=""
                />
              </div>

              <div class="itemMainaBottom">
                <!-- 简介 -->
                <div class="itemMainaText">{{ item.title }}</div>
                <!-- 个人信息 -->
                <div class="itemMainaInfo">
                  <!-- 个人图片 昵称 -->
                  <div class="itemMainaInfoLeft">
                    <img class="infoImg" :src="item.avatar" />
                    <div class="itemMainaInfoLeftText">{{ item.nickname }}</div>
                  </div>
                  <!-- 观看数量 -->
                  <div class="itemMainaInfoRight">
                    <img class="infoLook" src="https://yanxuan.nosdn.127.net/5097bc5f2e1eb15f2a32b56895db073a.png" />
                    <div class="infoMath">33k</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-list>
  </div>
</template>
<script lang="ts">
import { defineComponent, watch } from 'vue'
export default defineComponent({
  name: 'Shopping'
})
</script>
<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import { List } from 'vant'
import { Icon } from 'vant'
import { Swipe, SwipeItem } from 'vant'

import { useDeserveStore } from '@/stores/modules/deserve'
const deserveStore = useDeserveStore()

import cloneDeep from 'lodash/cloneDeep'
import type { navWapData, recAuto } from '@/api/deserve'
import { useRouter } from 'vue-router'

let navWap = ref<navWapData[]>([])

const router = useRouter()
const list = ref([])
const loading = ref(false)
const finished = ref(false)
// navWap.value = JSON.parse(JSON.stringify(deserveStore.navWap))
let recAuto
let nowRecAuto = ref<any>([])
const home = () => {
  router.push({ path: 'home' })
}
const category = () => {
  router.push({ path: 'search' })
}
const shopCart = () => {
  router.push({ path: 'shopCart' })
}
const getNav = async () => {
  await deserveStore.getNavWap()
  navWap.value = cloneDeep(deserveStore.navWap)
}
//获取RecAuto数据
let num = ref(1)
const getRec = async (page: number = 1) => {
  await deserveStore.getRecAuto(page)
  recAuto = cloneDeep(deserveStore.recAuto)
  recAuto.forEach((item: recAuto) => {
    item.topics.push(item.look)
    item.topics.forEach((iu: any) => {
      return nowRecAuto.value.push(iu)
    })
  })

  num.value++
}
const onLoad = async () => {
  await getRec(num.value)
  loading.value = false
}
// //滚动条高度
// let scrolla = ref()
// //视图总长
// let innerHeighta = ref()
// // 实时滚动条高度
// const scrollTop = () => {
//   let scroll = document.documentElement.scrollTop || document.body.scrollTop
//   scrolla.value = scroll
//   let a = document.body.clientHeight
//   innerHeighta.value = a
// }
onMounted(() => {
  getNav()
  getRec(num.value)

  // console.log(screenHeight.value)
  // 监听滚动条位置
  // window.addEventListener('scroll', scrollTop, true)
})
// watch(scrolla, (newVal, oldVal) => {
//   // console.log(newVal, innerHeighta.value - innerHeight - 0.7)

//   if (newVal + innerHeight >= Math.floor(innerHeighta.value - 0.5)) {
//     getRec(num.value)
//   }
// })
</script>

<style lang="less" scoped>
.custom-indicator {
  position: absolute;
  right: 38%;
  bottom: 5px;
  height: 3px;
  width: 80px;
  background: rgba(0, 0, 0, 0.1);
  display: flex;
}
.custom-indicatorItem {
  width: 20px;
  flex-shrink: 0;
  &.active {
    background: rgb(246, 105, 105);
  }
}
body {
  height: 100%;
}
.container {
  background-color: #eeeeee;
  height: 100%;
  width: 100%;
  overflow: hidden;
  .valueHeader {
    width: 92%;
    height: 50px;
    display: flex;
    justify-content: space-between;
    font-size: 19px;
    align-items: center;
    padding: 0 12px;
    .valueHeaderRight {
      width: 70px;
      display: flex;
      justify-content: space-between;
    }
  }
  .nav {
    position: relative;
    height: 270px;
    padding-top: 73px;
    .navHeader {
      position: absolute;
      top: 0;
      z-index: 1;
      height: 263px;
      .navTitleImg {
        position: absolute;
        width: 65px;
        height: 35px;
        z-index: 2;
        top: 30px;
        left: 7px;
      }
      .navTitle {
        position: absolute;
        top: 43px;
        left: 75px;
        font-size: 15px;
        color: #fff;
      }
      .navImg {
        width: 100%;
        height: 259 px;
      }
    }
    .navMainBox {
      margin: 0 10px;
      height: 270px;
      position: absolute;
      width: 100%;
      background-color: #fff;
      z-index: 2;
      border-radius: 10px;
      width: 352px;
      .swiperItem {
        padding: 18px 0 3px 0;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        text-align: center;
        .swiperItemMain {
          width: 100%;
          height: 100px;
          .swiperItemImg {
            width: 60px;
            height: 60px;
          }
        }
      }
    }
  }
}
.navMainSwiper {
  display: flex;
  width: 352px;
  height: 270px;
}
.swiperItemTittle {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .swiperItemTittleTop {
    font-size: 14px;
  }
  .swiperItemTittleBottom {
    font-size: 6px;
  }
}

.masonryBox {
  margin-top: 10px;
  margin-left: 9px;

  display: flex;
  width: 100%;
  display: flex;
  .masonryBoxMain {
    width: 95%;
    height: 100%;
    .item {
      position: relative;
      width: 48%;
      background: #ffffff;
      border-radius: 8px;
      .itemMain {
        width: 100%;
        border-radius: 8px;
        flex-direction: column;
        .itemMainImg {
          width: 100%;
          border-radius: 8px;
        }
        .itemMainImgOne {
          width: 100%;
          border-radius: 8px;
        }
        .videoIcon {
          position: absolute;
          height: 40px;
          bottom: 60%;
          left: 40%;
          width: 40px;
        }
        .itemMainaBottom {
          .itemMainaText {
            padding: 4px 8px 0 8px;
            font-size: 15px;
          }
          .itemMainaInfo {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 9px 8px 12px 8px;
            .itemMainaInfoLeft {
              display: flex;
              align-items: center;
              .infoImg {
                width: 24px;
                height: 24px;
                border-radius: 50%;
              }
              .itemMainaInfoLeftText {
                color: #7f7f7f;
                font-size: 12px;
                margin-left: 3px;
              }
            }
            .itemMainaInfoRight {
              color: #7f7f7f;
              display: flex;
              font-size: 10px;
              .infoLook {
                width: 16px;
                height: 16px;
              }
            }
          }
        }
      }
    }
  }
}
</style>
